package com.example.scenictourback.controller;

import com.example.scenictourback.common.Result;
import com.example.scenictourback.dto.HotelDTO;
import com.example.scenictourback.entity.Hotel;
import com.example.scenictourback.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    /**
     * 获取所有酒店列表
     */
    @GetMapping("/all")
    public Result<?> listAllHotels() {
        List<HotelDTO> hotels = hotelService.listAllHotels();
        return Result.success(hotels);
    }

    /**
     * 根据景区ID获取附近酒店列表
     */
    @GetMapping("/list")
    public Result<?> listHotelsByScenicAreaId(@RequestParam Integer scenicAreaId) {
        List<HotelDTO> hotels = hotelService.listHotelsByScenicAreaId(scenicAreaId);
        return Result.success(hotels);
    }

    /**
     * 根据ID获取酒店详情
     */
    @GetMapping("/{id}")
    public Result<?> getHotelById(@PathVariable Integer id) {
        HotelDTO hotel = hotelService.getHotelById(id);
        return Result.success(hotel);
    }

    /**
     * 根据名称搜索酒店
     */
    @GetMapping("/search")
    public Result<?> searchHotelsByName(@RequestParam String name) {
        List<HotelDTO> hotels = hotelService.searchHotelsByName(name);
        return Result.success(hotels);
    }

    /**
     * 新增酒店（管理员权限）
     */
    @PostMapping("/add")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> addHotel(@RequestBody Hotel hotel) {
        HotelDTO savedHotel = hotelService.addHotel(hotel);
        return Result.success("添加成功", savedHotel);
    }

    /**
     * 更新酒店信息（管理员权限）
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> updateHotel(@PathVariable Integer id, @RequestBody Hotel hotel) {
        HotelDTO updatedHotel = hotelService.updateHotel(id, hotel);
        return Result.success("更新成功", updatedHotel);
    }

    /**
     * 删除酒店（管理员权限）
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<?> deleteHotel(@PathVariable Integer id) {
        hotelService.deleteHotel(id);
        return Result.success("删除成功");
    }
} 